﻿using System.Collections.Generic;
using KC.Framework.Tenant;
using KC.Service.Core.DTO.Dict;

namespace KC.Service.Core.WebApiService.Business
{
    public interface IDictionaryApiService
    {
        /// <summary>
        /// 获取省份信息
        /// </summary>
        /// <returns></returns>
        List<ProvinceDTO> GetAllProvinces();

        /// <summary>
        /// 获取城市信息
        /// </summary>
        /// <returns></returns>
        List<CityDTO> GetAllCities();
        /// <summary>
        /// 根据省份Id，获取城市信息
        /// </summary>
        /// <returns></returns>
        List<CityDTO> GetCitiesByProvinceId(int provinceID);

        /// <summary>
        /// 获取国民经济行业分类信息
        /// </summary>
        /// <returns></returns>
        List<IndustryClassficationDTO> GetRootIndustryClassfications();

        /// <summary>
        /// 获取手机归属地信息
        /// </summary>
        /// <param name="mobilePhone">手机号码</param>
        /// <returns></returns>
        MobileLocationDTO GetMobileLocation(string mobilePhone);
    }

    public class DictionaryApiService : IdSrvOAuth2ClientRequestBase, IDictionaryApiService
    {
        private const string ServiceName = "KC.Service.Core.WebApiService.DictionaryApiService";

        public DictionaryApiService(
            Tenant tenant,
            System.Net.Http.IHttpClientFactory httpClient,
            Microsoft.Extensions.Logging.ILogger<AccountApiService> logger)
            : base(tenant ?? TenantConstant.DbaTenantApiAccessInfo, httpClient, logger)
        {
        }

        /// <summary>
        /// 获取省份信息
        /// </summary>
        /// <returns></returns>
        public List<ProvinceDTO> GetAllProvinces()
        {
            ServiceResult<List<ProvinceDTO>> result = null;
            WebSendGet<ServiceResult<List<ProvinceDTO>>>(
                ServiceName + ".GetAllProvinces",
                SsoApiServerUrl + "DictionaryApi/GetAllProvinces",
                ApplicationConstant.DicScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<ProvinceDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        /// <summary>
        /// 获取城市信息
        /// </summary>
        /// <returns></returns>
        public List<CityDTO> GetAllCities()
        {
            ServiceResult<List<CityDTO>> result = null;
            WebSendGet<ServiceResult<List<CityDTO>>>(
                ServiceName + ".GetAllCities",
                SsoApiServerUrl + "DictionaryApi/GetAllCities",
                ApplicationConstant.DicScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<CityDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        /// <summary>
        /// 获取手机归属地信息
        /// </summary>
        /// <param name="mobilePhone">手机号码</param>
        /// <returns></returns>
        public List<CityDTO> GetCitiesByProvinceId(int provinceID)
        {
            ServiceResult<List<CityDTO>> result = null;
            WebSendGet<ServiceResult<List<CityDTO>>>(
                ServiceName + ".GetCitiesByProvinceId",
                SsoApiServerUrl + "DictionaryApi/GetCitiesByProvinceId?provinceID=" + provinceID,
                ApplicationConstant.DicScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<CityDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        /// <summary>
        /// 获取国民经济行业分类信息
        /// </summary>
        /// <returns></returns>
        public List<IndustryClassficationDTO> GetRootIndustryClassfications()
        {
            ServiceResult<List<IndustryClassficationDTO>> result = null;
            WebSendGet<ServiceResult<List<IndustryClassficationDTO>>>(
                ServiceName + ".GetRootIndustryClassfications",
                SsoApiServerUrl + "DictionaryApi/GetRootIndustryClassfications",
                ApplicationConstant.DicScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<IndustryClassficationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        /// <summary>
        /// 获取手机归属地信息
        /// </summary>
        /// <param name="mobilePhone">手机号码</param>
        /// <returns></returns>
        public MobileLocationDTO GetMobileLocation(string mobilePhone)
        {
            ServiceResult<MobileLocationDTO> result = null;
            WebSendGet<ServiceResult<MobileLocationDTO>>(
                ServiceName + ".GetMobileLocation",
                SsoApiServerUrl + "DictionaryApi/GetMobileLocation?mobilePhone=" + mobilePhone,
                ApplicationConstant.DicScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<MobileLocationDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
    }
}
